<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2019/3/14
 * Time: 上午12:03
 */

namespace App\Http\Controllers\Admin;


use App\Models\Hotel;
use App\Models\MachineGoodsModel;
use App\Models\MachineModel;
use Illuminate\Http\Request;

class HotelController
{
    public function index()
    {
        $hotel = Hotel::list();
        return view('admin.hotel.index',[
            'edit_url'  => url('admin/hotel/edit'),
            'del_url'   => url('admin/hotel/delete_hotel'),
            'list'     => $hotel,
            'pages'     => $hotel -> render(),
        ]);
    }

    /**酒店下的商品列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function hotel_goods_view(Request $request,$hotel_id = 0)
    {
        return view('admin.hotel.hotel_goods',[
            'hotel_id' => empty($hotel_id) ? 0 :$hotel_id
        ]);
    }

    public function edit_view(Request $request,$hotel_id)
    {
        $hotel = Hotel::query()->where('hotel_id','=',$hotel_id)->first()->toArray();
        return view('admin.hotel.edit',['list'=>$hotel]);
    }

    public function create_view()
    {
        return view('admin.hotel.create',[]);
    }

    /**酒店的新增或修改
     * @param Request $request
     * @param Hotel $hotel
     * @return \Illuminate\Http\RedirectResponse
     */
    public function save(Request $request,Hotel $hotel)
    {
        $res = $hotel::save_hotel($request->all());
        if($res){
            return redirect('admin/hotel/index')->with('success','更新成功');
        }else{
            return redirect('admin/hotel/index')->with('danger','更新失败');
        }
    }

    public function apply_template(Request $request,$hotel_id)
    {
        if(empty($hotel_id)){
            return redirect('admin/hotel/index')->with('success','批量应用模板成功');
        }

        $hotel_id_arr = explode(',',$hotel_id);
        foreach($hotel_id_arr as $hotel_id){
           $res =  MachineGoodsModel::apply_template($hotel_id,'');
           if(!$res){
               return redirect('admin/hotel/index')->with('danger',MachineGoodsModel::$error);
           }
        }
        return redirect('admin/hotel/index')->with('success','批量应用模板成功');
    }


    public function delete_hotel(Request $request,$hotel_id)
    {
        if(empty($hotel_id)){
            return redirect('admin/hotel/index');
        }

        if(!Hotel::delete_hotel($hotel_id)){
            return redirect('admin/hotel/index')->with('danger','删除酒店失败');
        }
        return redirect('admin/hotel/index')->with('success','删除酒店成功');
    }





}